package com.fenghuajueli.module_user.vip

import android.text.TextUtils
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo
import com.fenghuajueli.libbasecoreui.constants.SwitchKeyConstants
import com.fenghuajueli.libbasecoreui.data.entity.goods.GoodsEntity
import com.fenghuajueli.module_user.network.repository.TemplateRepository
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

/**
 * @Author: sz_ijj
 * @Date: 2024-01-08 17:16
 **/
class VipViewModel : ViewModel() {
    val templateResult = TemplateRepository.templateData.asSharedFlow()
    val currentGood = MutableStateFlow<GoodsEntity?>(null)

    private val _buttonUiState = MutableStateFlow(ButtonUiState())
    val buttonUiState = _buttonUiState.asStateFlow()

    private val _goodsList = MutableStateFlow<List<GoodsEntity>>(emptyList())
    val goodsList = _goodsList.asStateFlow()

    private val _updateInfoEvent = MutableSharedFlow<Any>()
    val updateInfoEvent = _updateInfoEvent.asSharedFlow()

    fun setGoodsList(list: List<GoodsEntity>) = viewModelScope.launch {
        if (SwitchKeyUtils.getPayChannelType() == 2) {
            list.filterNot { it.vip_type > 100 }
                .apply {
                    _goodsList.emit(this)
                    setCurrentGood(this.first())
                    return@launch
                }
        }
        _goodsList.emit(list)
        setCurrentGood(list.first())
    }

    fun setCurrentGood(goodsEntity: GoodsEntity) = viewModelScope.launch {
        currentGood.emit(goodsEntity)
        updateButtonData()
    }

    fun updateButtonData() = viewModelScope.launch {
        var aliPay = true
        var wechatPay = true
        var subscribe = false
        when (SwitchKeyUtils.getPayChannelType()) {
            2 -> aliPay = false
            3 -> wechatPay = false
        }

        if (SwitchKeyUtils.getWechatPayType() != SwitchKeyConstants.WECHAT_PAY_TYPE_H5 &&
            (AppConfigInfo.WECHAT_APP_ID.startsWith("xxxx") ||
                    AppConfigInfo.WECHAT_APP_SECRET.startsWith("xxxx") ||
                    TextUtils.isEmpty(AppConfigInfo.WECHAT_APP_ID) ||
                    TextUtils.isEmpty(AppConfigInfo.WECHAT_APP_SECRET))
        ) {
            wechatPay = false
        }

        currentGood.value?.let {
            if (it.vip_type > 100) {
                aliPay = true
                wechatPay = false
                subscribe = true
            }
        }

        _buttonUiState.update {
            it.copy(
                wechatPay = wechatPay,
                aliPay = aliPay,
                subscribe = subscribe,
                isCheck = false,
                payChannel = if (wechatPay) AppConfigInfo.PAY_CHANNEL_WECHAT else AppConfigInfo.PAY_CHANNEL_ZFB
            )
        }
    }

    fun updateUserInfo() = viewModelScope.launch {
        _updateInfoEvent.emit(Unit)
    }

    fun checkCurrentGoods() = viewModelScope.launch {
        _buttonUiState.update {
            it.copy(isCheck = !it.isCheck)
        }
    }

    fun checkPayChannel(payChannel: String) = viewModelScope.launch {
        _buttonUiState.update {
            it.copy(payChannel = payChannel)
        }
    }
}

data class ButtonUiState(
    val wechatPay: Boolean = true,
    val aliPay: Boolean = true,
    val subscribe: Boolean = false,
    val isCheck: Boolean = false,
    val payChannel: String = AppConfigInfo.PAY_CHANNEL_WECHAT,
)